<?php

namespace App\Handles;

use EasyWeChat\Factory;


/**
 * 微信交互相关类
 */
class WechatHandle
{
    protected $app; //请求的实例
    public $config;

    public function __construct()
    {
        $this->config = [
            'app_id' => 'wxea17d667b9e895fc',
            'secret' => 'e45fc5ca97f9e667ac949416cd50e004',

            // 下面为可选项
            // 指定 API 调用返回结果的类型：array(default)/collection/object/raw/自定义类名
            'response_type' => 'array',

            'log' => [
                'level' => 'debug',
                'file' => __DIR__ . '/wechat'.date('Ymd').'.log',
            ],
        ];
    }


    /**
     * 小程序登录
     */
    public function wxLogin($code)
    {
        $app = Factory::miniProgram($this->config);
        return $app->auth->session($code);
    }

    /**
     * 微信小程序消息解密
     * 
     * 比如获取电话等功能，信息是加密的，需要解密。
     * 
     */
    public function decryptData($session, $iv, $encryptedData)
    {
        $app = Factory::miniProgram($this->config);
        return $decryptedData = $app->encryptor->decryptData($session, $iv, $encryptedData);       

    }
}
